﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using System.IO;
using System.Web.Services;

namespace TMDT_Nhom1A.Manager
{
    public partial class ThemSanPham_Manager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dlparentcatalogue.DataSource = ParentCatalogue_BUS.ListKeyCatalogue();
                dlparentcatalogue.DataValueField = "ParentCatalogue_ID";
                dlparentcatalogue.DataTextField = "ParentCatalogue_Name";
                dlparentcatalogue.DataBind();
                dlbrand.DataSource = Brand_BUS.ListBrand();
                dlbrand.DataTextField = "Brand_Name";
                dlbrand.DataValueField = "Brand_ID";
                dlbrand.DataBind();
                dlCurrency.DataSource = ElectricDevice_BUS.GetCurrencyOfProduct();
                dlCurrency.DataValueField = "Currency_ID";
                dlCurrency.DataTextField = "Currency_Name";
                dlCurrency.DataBind();

                dlparentcatalogue_SelectedIndexChanged(sender, e);
            }
        }

        protected void dlparentcatalogue_SelectedIndexChanged(object sender, EventArgs e)
        {

            dlchildrencatalogue.DataSource = ChildCatalogue_BUS.ListCatalogue(int.Parse(dlparentcatalogue.SelectedValue));
            dlchildrencatalogue.DataTextField = "ChildCatalogue_Name";
            dlchildrencatalogue.DataValueField = "ChildCatalogue_ID";
            dlchildrencatalogue.DataBind();

        }

        [WebMethod]
        public static bool IsUserAvailable(string namedevice)
        {

            bool kq = ElectricDevice_BUS.CheckNameElectricDevice(namedevice);

            if (kq == true)
            {
                return false;
            }
            else
            {
                return true;
            }


        }

        protected void btAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (tbelectricdevicename.Text != "" && ElectricDevice_BUS.CheckNameElectricDevice(tbelectricdevicename.Text))
            {

                ELECTRIC_DEVICE electricdevice = new ELECTRIC_DEVICE();
                electricdevice.ElectricDevice_ID = tbelectricdevicename.Text;
                electricdevice.ElectricDevice_Name = tbelectricdevicename.Text;

                electricdevice.InsertedDate = DateTime.Now;
                electricdevice.NumberOfBuyers = 0;
                electricdevice.Quanlity = tbquanlity.Text;
                electricdevice.Status = 1;
                electricdevice.Size = tbsize.Text;
                electricdevice.Brand_ID = int.Parse(dlbrand.SelectedValue);
                electricdevice.ChildCatalogue_ID = int.Parse(dlchildrencatalogue.SelectedValue);
                electricdevice.Currency_ID = dlCurrency.SelectedValue;
                try
                {
                    electricdevice.Quantity = int.Parse(tbquantity.Text);
                    electricdevice.RetailPrice = tbRetailPrice.Text;
                    electricdevice.DealerPrice = tbDealerPrice.Text;
                    electricdevice.Description = tbdescription.Text;
                    if (checkFileType(FileUpload1.FileName))  //Check for file types
                    {

                        FileInfo f = new FileInfo(FileUpload1.FileName);
                        string fileName;
                        //long lenght = f.Length;
                        if (FileUpload1.PostedFile.ContentLength < 50 * 1024)
                        {
                            fileName = electricdevice.ElectricDevice_ID + f.Extension;
                            electricdevice.ImagePath = fileName;
                            FileUpload1.SaveAs(MapPath("../images/" + fileName));
                            ElectricDevice_BUS.AddProduct(electricdevice);
                            string url = "ChiTietSanPham_Manager.aspx?ElectricDeviceID=" + electricdevice.ElectricDevice_ID;
                            Alert.ShowAndRedirect("Đã thêm sản phẩm thành công !", url);

                        }
                        else
                        {
                            Alert.Show("Kích thước file phải nhỏ hơn 50kb !");
                        }
                    }
                    else
                    {
                        Alert.Show("Kiểu file không đúng. vui lòng upload file : .jpg , .gif, .png");
                    }
                }
                catch (Exception ex)
                {
                    Alert.Show("Vui lòng nhập số lượng và giá !");
                }

            }
            else
            {
                Alert.Show("Chưa nhập tên sản phẩm hoặc tên đã tồn tại !");
            }
        }

        private bool checkFileType(string fileName)
        {
            string fileExt = Path.GetExtension(FileUpload1.FileName);
            switch (fileExt.ToLower())
            {
                case ".png":
                    return true;
                case ".jpg":
                    return true;
                case ".gif":
                    return true;
                default:
                    return false;
            }

        }
    }
}
